package org.jeecg.modules.supplier.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.jeecg.modules.supplier.entity.Supplier;
import org.jeecg.modules.supplier.entity.qualification;

import java.util.List;
@Mapper
public interface SupplierMapper extends BaseMapper<Supplier> {

    @Select("SELECT s.*, " +
            "COUNT(se.id) as evaluation_count, " +
            "AVG(se.total_score) as avg_score " +
            "FROM proc_supplier s " +
            "LEFT JOIN proc_supplier_evaluation se ON s.id = se.supplier_id " +
            "WHERE s.audit_status = '1' " +
            "AND (#{category} IS NULL OR FIND_IN_SET(#{category}, s.engineering_categories) > 0) " +
            "GROUP BY s.id " +
            "ORDER BY s.create_time DESC")
    IPage<Supplier> selectSupplierWithEvaluation(Page<Supplier> page, @Param("category") String category);

    @Select("SELECT * FROM proc_supplier " +
            "WHERE audit_status = '1' " +
            "AND (#{category} IS NULL OR FIND_IN_SET(#{category}, engineering_categories) > 0) " +
            "ORDER BY winning_rate ASC, total_score DESC")
    List<Supplier> selectSuppliersForLottery(@Param("category") String category);

    @Select("SELECT COUNT(*) FROM proc_supplier WHERE credit_code = #{creditCode} AND id != #{id}")
    int checkCreditCodeExists(@Param("creditCode") String creditCode, @Param("id") String id);

    @Select("SELECT COUNT(*) FROM proc_qualification WHERE supplier_id = #{supplierId} ")
    List<qualification> getQualificationList(String supplierId);


    @Select("SELECT credit_code FROM proc_supplier WHERE id = #{supplierId} ")
    String getcreditCodeBySupplierId(String supplierId);
}
